package ca.swank.hq.ai;

import ca.swank.hardquor.*;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Comparator;

import java.util.Random;

/**
 * a simple implementation of the hardquorUserGame interface for creating AI players
 *
 * This implementation takes care of tracking the board for the AI players and provides
 * implementations of the logic that is not game related such as chatting.
 */
public class hqSimpleAIGame implements hardquorUserGame
{
  // network logic
  // the gameClient for sending game commands
  protected hardquorGameClient gameClient;
  protected String userName;

  // game logic
  protected CachingSmartBoard board;
  protected static Random dice = new Random(System.currentTimeMillis());
  // which player I am
  protected int me;

  // makes funny comments
  protected static hqSimpleTalker talker = new hqSimpleTalker("resources/comments.txt");

  private String[] errorNames = new String[] {"ERROR_NONE", "ERROR_PLAYER_BLOCKED",
   "ERROR_INVALID_MOVE", "ERROR_NOT_ADJACENT", "ERROR_WRONG_PLAYER", "ERROR_OCCUPIED_TARGET",
   "ERROR_INSUFFICIENT_WALLS", "ERROR_GAME_COMPLETE", "ERROR_WALL_CRISSCROSS" };

  private static final String COMMAND_SHOWBOARD = ":board";


  /** 
   * construct a new hqSimpleAIGame which can make moves, respond to moves made
   * and comment on the game
   */
  public hqSimpleAIGame(hardquorGameClient gameClient, int me, String userName)
  { this.gameClient = gameClient;
    this.me = me;
    this.userName = userName;
    board = new CachingSmartBoard();
  }

  /**
   * a chat message received from the specified user. Chat messages should
   */
  public void notifySpeak(String user, String message)
  { System.out.println(user + ": " + message);

    // check comment for a command
    if(message.equalsIgnoreCase(COMMAND_SHOWBOARD))
    { System.out.println(board.toString());

    // regular comments get added to the comment pool
    } else if(!user.equals(userName))
    { talker.addComment(message);
    }

  }

  /**
   * notification that a move required by the current player.
   *
   * this currently makes the simplest forward walking move. This method should
   * be extended for smarter AI implementations
   */
  public void notifyTurn()
  { ArrayList forwardMoves = board.getForwardWalkMoves(board.getPlayerLocation(me), me);
    WalkMove bestMove = (WalkMove)forwardMoves.get(dice.nextInt(forwardMoves.size()));
    gameClient.tryMove(bestMove.getSource(), bestMove.getTarget());
  }

  /**
   * notification of the winner of the current game.
   */
  public void notifyWinner(String winnerName)
  { System.out.println("the winner of the game is " + winnerName);
  }

  /**
   * a response to a move that has been made. An errorcode is notified
   */
  public void notifyError(int errorCode)
  { System.out.println("error " + errorNames[errorCode] + " has occured");
  }

  /**
   * notification that a walk move has been made. If the implementation
   */
  public void notifyMove(int[] source, int[] target)
  { board.makeMove(source, target);
  }

  /**
   * notification that a wall move has been made. If the implementation
   */
  public void notifyMove(int[] a1, int[] a2, int[] b1, int[] b2)
  { board.makeMove(a1, a2, b1, b2);
  }

  /**
   * notification that the specified user has quit the game. Note that his may occur
   */
  public void notifyQuit(String user)
  { System.out.println("user " + user + " has quit the game");
  }

}

